<?php

	include("common.php");
	
	if(!empty($_POST)){
		mysql_query("DELETE FROM `197_calendar` WHERE `date` = '" . $_POST['date'] . "'");
		foreach($_POST['calendar'] as $id => $row){
			mysql_query("INSERT INTO `197_calendar` ( `content`, `date`, `description` ) VALUES ( '" . $row . "', '" . $_POST['date'] . "', '" . $_POST['description'][$id] . "' )");
		}
		
		include("header.php");
		
?>
<h1>修改行事曆</h1>
<p>修改成功，請<a href="calendarEdit.php?date=<?php echo $_POST['date']; ?>">按此</a>返回。</p>
<?php

		include("sidebar.php");
		include("footer.php");
		exit;
	}
	
	$result = mysql_query("SELECT * FROM `197_calendar` WHERE `date` = '" . $_GET['date'] . "'");
	$calendar = array();
	while($row = mysql_fetch_assoc($result)){
		array_push($calendar, $row);
	}
	
?>
<?php include("header.php"); ?>
<script type="text/javascript">
	function addcalendarItem(){
		$("#calendar-list").append('<li><input type="text" size="25" name="description[]" value="<?php echo htmlspecialchars($row['description']); ?>" /> - <input type="text" size="50" name="calendar[]" /></li>');
	}
</script>
<div id="delete-calendar-dialog"><p>確定刪除？</p></div>
<script type="text/javascript" defer="defer">
	$("#delete-calendar-dialog").dialog({
		width: 300,
		height: 210,
		autoOpen: false,
		title: "刪除聯絡事項",
		modal: true,
		draggable: false,
		resizable: false
	});
	function showConfirm(cl_id){
		$("#delete-calendar-dialog").dialog("option", "buttons", {"確定": function(){ $(this).dialog("close"); deleteCL(cl_id); }, "取消": function(){ $(this).dialog("close"); }
		}).dialog("open");
	}
	function deleteCL(cl_id){
		$("#delete-calendar-dialog").dialog("option", "buttons", { "關閉": function(){ location.reload(); } });
		$("#delete-calendar-dialog").html("<img src='../images/ajax-loader.gif' />");
		$("#delete-calendar-dialog").dialog("open");
		$("#delete-calendar-dialog").load("calendarDelete.php?calendar_id=" + cl_id + "&ajax=true");
	}
</script>
<h1><?php echo $_GET['date']; ?> 行事曆</h1>
<form action="calendarEdit.php" method="POST">
	<ul id="calendar-list">
		<?php foreach($calendar as $row){ ?>
			<li><input type="text" size="25" name="description[]" value="<?php echo htmlspecialchars($row['description']); ?>" /> - <input type="text" size="50" name="calendar[]" value="<?php echo htmlspecialchars($row['content']); ?>" /> - [ <a href="calendarDelete.php?calendar_id=<?php echo $row['calendar_id']; ?>" onclick="showConfirm(<?php echo $row['calendar_id']; ?>); return false;">刪除</a> ]</li>
		<?php } ?>
	</ul>
	<input type="submit" value="確定" />｜<a href="javascript:void(0)" onclick="addcalendarItem();">新增</a><input type="hidden" name="date" value="<?php echo $_GET['date']; ?>" />
</form>
<?php include("sidebar.php"); ?>
<?php include("footer.php"); ?>